package oneapm

import (
	"runtime/debug"
	"sync"

	"github.com/coocood/freecache"
)

const (
	pbContentType   = "application/x-protobuf"
	jsonContentType = "application/json"
)

type LoggerOneApm interface {
	Info(key string, value []byte)
	Debug(key string, value []byte)
	Error(key string, err error)
}

var loggerOneApm LoggerOneApm
var cache *freecache.Cache

// InitOne 初始化函数，加载日志、缓存大小（Byte），gc回收相关（%）
func InitOne(logger LoggerOneApm, cacheSize int, gcPercent int) {
	if cacheSize == 0 {
		cacheSize = 1024 * 1024 * 1024 //1G
	}
	if gcPercent == 0 {
		gcPercent = 20
	}
	var once sync.Once
	onceBody := func() {
		cache = freecache.NewCache(cacheSize) // 提前申请缓存
		debug.SetGCPercent(gcPercent)         // 当缓存量达到设置缓存的gcPercent%，启动gc
		loggerOneApm = logger
	}
	once.Do(onceBody)
}
